<?php

namespace App\Module\Api\Controllers;
use App\Exceptions\Customize\ParamsException;
use App\Module\Api\Controllers\Request\UserUpdateRequest;
use App\Module\Api\Validator\UserDetailsValidator;
use App\Module\Api\Validator\UserUpdateValidator;
use App\System\Base\Model\BaseImage;
use App\System\Base\Services\TokenService;
use App\System\Cis\Repositories\CisUserRepository;

/**
 * 更新用户详细
 * @package App\Module\Admin\Controllers
 */
class UserUpdateController extends Controller
{

    public function __construct()
    {
        $this->query = new UserUpdateRequest();
        parent::__construct();

    }

    public function index()
    {
        (new UserUpdateValidator())->check();
        $user_id = TokenService::getUserId();
        $userObj = $this->query->user;
        $user = CisUserRepository::getInstance();
        if(isset($userObj['image']) && $userObj['image'])
        {
           $image =  BaseImage::where('id',$userObj['image'])->first();
           if($image) $userObj['image'] = $image->path.$image->filename;
        }
        $set_array = ['name'=>'name','id_name'=>'idName','head_image_path'=>'image'];
        $set = [];
        foreach ($set_array as $k=>$v)
        {
            if(isset($userObj[$v]) && $userObj[$v])
            {
                $set[$k] = $userObj[$v];
            }

        }
        if($set)
        {
            $user->updateUser($set,$user_id);
        }
        return apiResponse();
    }
}
